
          /*
           -------ProductDao By Raj Vallamsetla------
  		   
				For the insertion of Base Product 

          */

package com.scalar.chiptrack.operations.process.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.utils.DBLoader;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class SortProcessSplitDAO 
{

 private static DBLoader s_dbLoader = null;

 
     public SortProcessSplitDAO()
	  {
	
	  }




  public void save(DBTransaction dbTransaction,String trackId, String processStepType ,String status) throws ScalarException  {


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
               
				 		

		try{
				
								  
          if (dbTransaction == null) {
                            conn = DataSource.getConnection();
                        }
                        else {
                            conn = dbTransaction.getConnection();
                        }	



		  pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.sortProcessInsert"));
		  pstmt.setString(1,trackId);
		  pstmt.setString(2,processStepType);
		  pstmt.setString(3,status);

		  pstmt.executeUpdate(); 
   			  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save Sort Process Split ", sqle);
        }

			finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
        }


		

	public String getSort(String trackId) throws ScalarException  {


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String checkSort = "";
                

		
		try{
				
		
        conn = DataSource.getConnection();                    
		//System.out.println("Conection "+conn);
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.selectSort"));
		pstmt.setString(1,trackId);
		rs=pstmt.executeQuery();

		while(rs.next()){
		checkSort ="exists";		     
		}


	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("In Get Sort Data Base", sqle);
        }

		finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
			
			return checkSort;
		
        }


	public String  checkSortTrackId(String[] fabTrackList) throws ScalarException
    {
		
		         Connection conn = null;
				 PreparedStatement pstmt = null;
				 ResultSet rs    = null;
				 String trackId   = "";
				 String fabName	  = "";
				 String sortTrackId = "";
							 
		try{
				
					  
       conn = DataSource.getConnection();
	   pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.sortTrackId"));

	  

	   pstmt.setString(1,fabTrackList[0]);
	   rs = pstmt.executeQuery();
		
		

		if(rs.next())
		{
		 sortTrackId = "Exists"	;
		 
		}
 
		  		    
		   }catch(SQLException sql){
			
			sql.printStackTrace();
			throw new ScalarException("tracking Sort failed ", sql);

		   } finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

			return sortTrackId;

} 








	


	public void deleteSort(String productId) throws ScalarException{


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
                

		//System.out.println(" In Delete fab......");						

		try{
				
		conn = DataSource.getConnection();
     
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.sortProcessDelete"));
		pstmt.setString(1,productId);
		pstmt.executeUpdate();

		conn.commit();     
	
	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save FabProcess SetUp from database", sqle);
        }


				finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
			
		
        }




		public void delete(String pageId){


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
                 
		try{
				
								  
            if (s_dbLoader == null) {
                s_dbLoader = DBLoader.getInstance();
            }

         conn = s_dbLoader.getConnection();
		 pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.fabProcessDeleteField"));
		 pstmt.setString(1,pageId);
		 pstmt.executeUpdate();
		 conn.commit();
			  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            //throw new ScalarException ("Could not save FabProcess SetUp from database", sqle);
        }


            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }
		


   
	

}	